package pswvclass.pswvandroid2.com.category;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.risk.RiskList;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/11/1.
 */
public class CategoryAdd extends Fragment implements View.OnClickListener {
    private String tag = "RiskAdd";
    private TextView name;
    private Button add_category;
    protected static final int SUCCESS = 0;
    protected static final int ERROR = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.pswv_category_add, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        name = (EditText) getView().findViewById(R.id.add_category_name);
        add_category = (Button) getView().findViewById(R.id.add_category);
        add_category.setOnClickListener(this);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case SUCCESS:
                    getFragmentManager()
                            .beginTransaction()
                            .addToBackStack(null)
                            .replace(R.id.realtabcontent, new CategoryAdd()).commit();
                    Toast.makeText(getContext(), "添加成功", Toast.LENGTH_SHORT).show();
                    // startActivity(new Intent(getContext(), RiskAdd.class));
                    break;
                case ERROR:
                    Toast.makeText(getContext(), "连接错误 ....", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }

        }

        ;

    };


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.add_category:
                final String name1 = name.getText().toString().trim();

                if (TextUtils.isEmpty(name1)) {
                    Toast.makeText(getContext(), "不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                new Thread() {
                    public void run() {
                        try {
                            HttpUtils hu = new HttpUtils();
                            String data2;
                            final String src = getResources().getString(R.string.src_mysql) + "pswv/api/categoryAcion/addCategory";
                            final Map<String, String> map1 = new HashMap<String, String>();
                            map1.put("name", name1);
                            data2 = hu.submitPostData(src, map1, "utf-8");
                            JSONObject jsonObj = new JSONObject(data2);
                            int result = Integer.parseInt(jsonObj.getString("code"));
                            if (result == 0) {
                                Message msg = Message.obtain();
                                msg.what = SUCCESS;
                                mHandler.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Message msg = Message.obtain();
                            msg.what = ERROR;
                            mHandler.sendMessage(msg);
                            Log.v(tag, e + "");
                        }
                    }
                }.start();
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_BACK) {
                    getFragmentManager()
                            .beginTransaction()
                            .addToBackStack(null)
                            .replace(R.id.realtabcontent, new CategoryList()).commit();
                    return true;
                }
                return false;
            }
        });
    }
}
